<?php

namespace App\Kuafu\Coupon\Controller\B;

use App\Exceptions\CustomValidateException;
use App\Kuafu\Coupon\Enum\CouponEnum;
use App\Kuafu\Coupon\Validate\CouponNewValidate;
use App\Kuafu\Coupon\Validate\CouponValidate;
use App\Kuafu\Coupon\Enum\CouponCodeEnum;
use App\Kuafu\Coupon\Service\CouponService;
use Illuminate\Http\JsonResponse;
use Illuminate\Http\Request;
use App\Kuafu\Base\Controller\BaseController;
use Psr\Container\ContainerExceptionInterface;
use Psr\Container\NotFoundExceptionInterface;

class CouponController extends BaseController
{
    /**
     * 根据id获取详情
     * @param $request
     * @param $configId
     */
    public function getCouponById(Request $request, $id)
    {
        $id    = (int)$id;
        $valid = CouponValidate::getCouponById(['id' => $id]);
        if ($valid) {
            return $this->fail(CouponCodeEnum::ERR_COUPON_PARAM_VALID, $valid);
        }
        $info = CouponService::getCouponById($id);

        return $this->success($info);
    }

    /**
     * 优惠券列表（活动选择券）
     * @param Request $request
     * @return JsonResponse
     */
    public function getCouponList(Request $request)
    {
        $startAt              = (string)$request->get('startAt', '');
        $name                 = (string)$request->get('name', '');
        $type                 = (string)$request->get('type', '');
        $channel              = (string)$request->get('channel', '');
        $status               = (string)$request->get('status', '');
        $filter               = (bool)(int)$request->get('filter', 0);
        $source               = (string)$request->get('source', '');
        $validate['pageSize'] = (int)$request->get('pageSize', 20);
        $validate['pageNum']  = (int)$request->get('pageNum', 1);
        $valid                = CouponValidate::getCouponList($validate);

        if ($valid) {
            return $this->fail(CouponCodeEnum::ERR_COUPON_PARAM_VALID, $valid);
        }

        return $this->success(CouponService::getCouponList($startAt, $name, $type, $channel, $status,
            $validate['pageSize'], $validate['pageNum'], $filter, $source));
    }

    /**
     * 新增
     * @param $request
     * @return JsonResponse
     */
    public function addCoupon(Request $request)
    {
        $valid = CouponValidate::addCoupon($request->all());
        if ($valid) {
            return $this->fail(CouponCodeEnum::ERR_COUPON_PARAM_VALID, $valid);
        }

        $coupon = CouponService::addCoupon($request->all());
        return $this->success($coupon->toArray());
    }

    /**
     * 删除（软删除）
     * @param $request
     * @param $configId
     */
    public function delCoupon(Request $request, $id)
    {
        $id    = (int)$id;
        $valid = CouponValidate::delCoupon(['id' => $id]);
        if ($valid) {
            return $this->fail(CouponCodeEnum::ERR_COUPON_PARAM_VALID, $valid);
        }

        CouponService::delCoupon($id);
        return $this->success();
    }

    /**
     * 根据id修改信息
     * @param Request $request
     * @param $id
     * @return JsonResponse
     * @throws \App\Kuafu\Coupon\Exceptions\CouponException
     */
    public function setCoupon(Request $request, $id)
    {
        $id    = (int)$id;
        $valid = CouponValidate::setCoupon($request->all() + ['id' => $id]);
        if ($valid) {
            return $this->fail(CouponCodeEnum::ERR_COUPON_PARAM_VALID, $valid);
        }

        CouponService::setCoupon($id, $request->all());
        return $this->success();
    }

    /**
     * 优惠券状态修改
     * @param Request $request
     * @param $id
     * @return JsonResponse
     * @throws \App\Kuafu\Coupon\Exceptions\CouponException
     */
    public function patchCouponStatus(Request $request, $id)
    {
        $status = $request->get('status');

        $id    = (int)$id;
        $valid = CouponValidate::patchCouponStatus($request->all() + ['id' => $id]);
        if ($valid) {
            return $this->fail(CouponCodeEnum::ERR_COUPON_PARAM_VALID, $valid);
        }

        CouponService::patchCouponStatus($id, $status);
        return $this->success();
    }

    public function getCouponRelease(Request $request, $couponId)
    {
        $name                 = (string)$request->get('name', '');
        $phone                = (int)$request->get('phone', '');
        $status               = (string)$request->get('status', '');
        $couponCode           = (int)$request->get('couponCode', '');
        $couponCollectionTime = (string)$request->get('name', '');
        $valid                = CouponValidate::getCouponRelease($couponId);

        if ($valid) {
            return $this->fail(CouponCodeEnum::ERR_COUPON_PARAM_VALID, $valid);
        }
    }

    /**
     * 优惠券编辑
     *
     * @param Request $request
     * @param int $id
     * @return JsonResponse
     * @throws CustomValidateException
     * @throws ContainerExceptionInterface
     * @throws NotFoundExceptionInterface
     */
    public function update(Request $request, int $id): JsonResponse
    {
        $params = CouponNewValidate::updateValidate($request->merge(["id" => $id])->all());
        CouponService::update($params);
        return $this->success();
    }
}
